﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DndTestBed
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool mMouseDown = false;
        Point mMouseDownPoint;

        public MainWindow()
        {
            InitializeComponent();

            ClearOutput();
        }

        private void OutputText(string text)
        {
            text = DateTime.Now + ": " + text;

            ListBoxItem item = new ListBoxItem();
            item.Content = text;
            mOutput.Items.Add(item);

            // select the last item so it has focus
            mOutput.ScrollIntoView(mOutput.Items[mOutput.Items.Count - 1]);
        }

        private void ClearOutput()
        {
            mOutput.Items.Clear();
        }

        private void mMenuFileExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void mMenuNetworkConnect_Click(object sender, RoutedEventArgs e)
        {
            OutputText("connecting...");
        }

        private void mMenuNetworkDisconnect_Click(object sender, RoutedEventArgs e)
        {
            OutputText("disconnecting...");
        }

        private void mMenuOutputClear_Click(object sender, RoutedEventArgs e)
        {
            ClearOutput();
        }

        private void mCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (mRectangle == mCanvas.InputHitTest(e.GetPosition(mCanvas)))
            {
                mMouseDown = true;
                mMouseDownPoint = e.GetPosition(mRectangle);
            }
        }

        private void mCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (mMouseDown && e.LeftButton == MouseButtonState.Pressed)
            {
                Point currentPoint = e.GetPosition(mCanvas);

                Canvas.SetLeft(mRectangle, currentPoint.X - mMouseDownPoint.X);
                Canvas.SetTop(mRectangle, currentPoint.Y - mMouseDownPoint.Y);
            }
        }

        private void mCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mMouseDown = false;
        }
    }
}
